﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.peddler {
    import flash.events.*;
    import flash.utils.*;
    import org.aswing.*;
    import Classes.*;
    import Classes.util.*;
    import com.greensock.*;
    import Display.FactoryUI.*;
    import Display.aswingui.*;

    public class PeddlerSlidePick extends SlidePick {

        public static const MARGIN_WIDTH:int = 30;
        public static const BUTTON_HEIGHT:int = 25;
        public static const INFO_PANE_HEIGHT:int = 30;
        public static const STATE_UNPERMITTED:int = 0;
        public static const STATE_PERMITTED:int = 1;

        protected var m_state:int;
        protected var m_info:JLabel;
        protected var m_info2:JLabel;
        protected var m_amountLabel:JLabel;
        protected var m_jLabel:JLabel;
        protected var m_uiHandle:int;
        protected var m_senderName:String;
        private var m_acceptBtn:CustomButton;
        private var m_havestBtn:CustomButton;
        private var m_expelBtn:CustomButton;
        private var m_cancelBtn:CustomButton;
        private var m_pdlrManager:PeddlerGroupManager;
        protected var m_isTopSlidePickOpened:Boolean = false;
        private var m_peddler:Peddler;
        private var m_pdlrTimer:Timer;
        private var m_plowedTime:uint;
        private var m_peddlerItemPhrase:Array;
        private var m_firstHarvestTime:Number;
        private var m_maxHarvestIncome:Number;
        private var m_expireTime:Number;
        public var moneyEarned:Number;

        public function PeddlerSlidePick(_arg1:PeddlerGroupManager, _arg2:Peddler, _arg3:Boolean=true, _arg4:Boolean=false, _arg5:int=0, _arg6:Boolean=false, _arg7:int=-1){
            var _local10:Object;
            var _local11:Player;
            this.m_pdlrManager = _arg1;
            this.m_peddler = _arg2;
            this.m_expireTime = ((Global.gameSettings.inGameDaySeconds * _arg2.peddlerModel.item.expireTime) * 1000);
            this.m_peddlerItemPhrase = _arg2.peddlerModel.item.phases.sortOn("ord");
            this.m_firstHarvestTime = _arg2.peddlerModel.item.growTime;
            this.m_maxHarvestIncome = _arg2.peddlerModel.item.coinYield;
            var _local8:uint;
            while (_local8 < this.m_peddlerItemPhrase.length) {
                _local10 = this.m_peddlerItemPhrase[_local8];
                _local10.duration = (Global.gameSettings.inGameDaySeconds * _local10.duration);
                _local8++;
            };
            this.m_jLabel = new JLabel("");
            this.m_jLabel.setFont(ASwingHelper.getBoldFont(14));
            this.m_jLabel.setPreferredWidth(250);
            var _local9 = "";
            if (this.m_peddler.peddlerModel.ownerId == Global.player.uid){
                _local9 = Global.player.snUser.picture;
            } else {
                _local11 = Global.player.findFriendById(this.m_peddler.peddlerModel.ownerId);
                _local9 = (_local11) ? _local11.snUser.picture : null;
            };
            super(_local9, _arg3, _arg4, _arg5, false, 250);
            addEventListener(MouseEvent.MOUSE_MOVE, this.stopMousePropagation, false, 0, true);
            addEventListener(MouseEvent.MOUSE_OUT, this.stopMousePropagation, false, 0, true);
            addEventListener(MouseEvent.MOUSE_OVER, this.stopMousePropagation, false, 0, true);
            addEventListener(MouseEvent.CLICK, this.onMouseClick, false, 0, true);
        }
        override protected function initInner():void{
            super.initInner();
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(AsWingConstants.CENTER);
            _local1.setPreferredWidth(slideWidth);
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanel(AsWingConstants.CENTER);
            _local2.setPreferredWidth(slideWidth);
            _local2.setPreferredHeight(INFO_PANE_HEIGHT);
            var _local3:JPanel = ASwingHelper.makeSoftBoxJPanel(AsWingConstants.CENTER, 10);
            if (((((!(Global.isVisiting())) && (!((this.m_peddler.peddlerModel.ownerId == Global.world.ownerId))))) && (!(this.m_peddler.peddlerModel.allowed)))){
                this.m_acceptBtn = new CustomButton(ZLoc.t("Dialogs", "Peddler_Permit"), null, "GreenSmallButtonUI");
                this.m_acceptBtn.setPreferredHeight(BUTTON_HEIGHT);
                this.m_acceptBtn.addActionListener(this.onAccept, 0, true);
                _local3.append(this.m_acceptBtn);
            };
            this.m_havestBtn = null;
            this.m_expelBtn = null;
            this.m_cancelBtn = null;
            if (((Global.isVisiting()) && ((this.m_peddler.peddlerModel.ownerId == Global.player.uid)))){
                this.m_havestBtn = new CustomButton(ZLoc.t("Dialogs", "Peddler_Harvest"), null, "GreenSmallButtonUI");
                this.m_havestBtn.setPreferredHeight(BUTTON_HEIGHT);
                this.m_havestBtn.addActionListener(this.onHavest, 0, true);
                this.m_havestBtn.visible = false;
                _local3.append(this.m_havestBtn);
            } else {
                if (((((!(Global.isVisiting())) && (!((this.m_peddler.peddlerModel.ownerId == Global.player.uid))))) && (!(this.m_peddler.peddlerModel.allowed)))){
                    this.m_expelBtn = new CustomButton(ZLoc.t("Dialogs", "Peddler_Expel"), null, "OrangeSmallButtonUI");
                    this.m_expelBtn.setPreferredHeight(BUTTON_HEIGHT);
                    this.m_expelBtn.addActionListener(this.onExpel, 0, true);
                    this.m_expelBtn.visible = false;
                    _local3.append(this.m_expelBtn);
                };
            };
            this.m_cancelBtn = new CustomButton(ZLoc.t("Dialogs", "OK"), null, "GreenSmallButtonUI");
            this.m_cancelBtn.setPreferredHeight(BUTTON_HEIGHT);
            this.m_cancelBtn.visible = false;
            _local3.append(this.m_cancelBtn);
            _local2.appendAll(10, this.m_jLabel);
            ASwingHelper.prepare(_local2);
            ASwingHelper.prepare(_local3);
            _local1.appendAll(_local2, _local3);
            ASwingHelper.prepare(_local1);
            innerAppendAll(_local1);
            this.onTimer();
        }
        public function updatePercentages():void{
            this.m_pdlrTimer = new Timer(1000);
            this.m_pdlrTimer.addEventListener(TimerEvent.TIMER, this.onTimer);
            this.m_pdlrTimer.start();
        }
        private function onTimer(_arg1:TimerEvent=null):void{
            var _local4:Number;
            var _local5:uint;
            var _local6:Object;
            var _local2:Number = (GlobalEngine.getTimer() - this.m_peddler.peddlerModel.counting_time);
            var _local3:Number = (this.getGrowTimeDelta(this.m_firstHarvestTime) - _local2);
            if (_local2 > this.m_expireTime){
                if (this.m_peddler){
                    this.m_peddler.removePick();
                    this.m_peddler.removePeddlerFromWorld();
                    return;
                };
            };
            if (_local3 < 0){
                this.moneyEarned = 0;
                _local4 = (_local2 / 1000);
                _local5 = 0;
                while (_local5 < this.m_peddlerItemPhrase.length) {
                    _local6 = this.m_peddlerItemPhrase[_local5];
                    if (_local4 > _local6.duration){
                        this.moneyEarned = (this.moneyEarned + ((_local6.duration * _local6.rate) / 60));
                        _local4 = (_local4 - _local6.duration);
                    } else {
                        this.moneyEarned = (this.moneyEarned + ((_local4 * _local6.rate) / 60));
                        break;
                    };
                    _local5++;
                };
                this.moneyEarned = int(this.moneyEarned);
                if (this.moneyEarned > this.m_maxHarvestIncome){
                    this.moneyEarned = this.m_maxHarvestIncome;
                    this.m_peddler.setIsOpenStatus(false);
                };
                if (this.m_expelBtn){
                    this.m_expelBtn.visible = true;
                };
                if (this.m_havestBtn){
                    this.m_havestBtn.visible = true;
                };
                if (this.m_cancelBtn){
                    this.m_cancelBtn.visible = false;
                };
                this.m_jLabel.setText(ZLoc.t("Dialogs", "Peddler_Slidepick_Earned_Counting", {
                    earned:String(this.moneyEarned),
                    max:String(this.m_maxHarvestIncome)
                }));
            } else {
                if (this.m_cancelBtn){
                    this.m_cancelBtn.visible = true;
                };
                this.m_jLabel.setText(ZLoc.t("Dialogs", "Peddler_Slidepick_Expel_Harvest_Time_Counting", {time:GameUtil.formatMinutesSeconds((_local3 / 1000))}));
            };
            if (((Global.isVisiting()) && (!((this.m_peddler.peddlerModel.ownerId == Global.player.uid))))){
                this.m_cancelBtn.visible = true;
            };
            if (((!(Global.isVisiting())) && (this.m_peddler.peddlerModel.allowed))){
                this.m_cancelBtn.visible = true;
            };
        }
        private function stopUpdatePercentages():void{
            if (this.m_pdlrTimer){
                this.m_pdlrTimer.stop();
                this.m_pdlrTimer.removeEventListener(TimerEvent.TIMER, this.onTimer);
                this.m_pdlrTimer = null;
            };
        }
        public function getGrowTimeDelta(_arg1:Number):Number{
            return (((_arg1 * Global.gameSettings.inGameDaySeconds) * 1000));
        }
        override protected function getPickBackground():Class{
            return (EmbeddedArt.trainUIPick);
        }
        override protected function getInnerBackground():Class{
            return (EmbeddedArt.trainUIBG);
        }
        public function onMouseClick(_arg1:MouseEvent=null):void{
            if (this.m_isTopSlidePickOpened){
                this.closeInnerPane();
            } else {
                this.openInnerPane();
            };
        }
        public function openInnerPane():void{
            if (((m_sliderActive) && (!(this.m_isTopSlidePickOpened)))){
                if (((this) && (this.parent))){
                    this.parent.setChildIndex(this, (this.parent.numChildren - 1));
                };
                TweenLite.to(m_innerSprite, 0.3, {
                    x:m_offXPos,
                    onComplete:this.buildAndShowInnerPane
                });
            };
        }
        protected function buildAndShowInnerPane():void{
            this.rebuildInner();
            this.showInnerPane();
            this.m_isTopSlidePickOpened = true;
        }
        public function closeInnerPane():void{
            if (((m_sliderActive) && (this.m_isTopSlidePickOpened))){
                TweenLite.to(m_innerSprite, 0.3, {
                    x:m_offXPos,
                    onComplete:this.closedInnerPane
                });
            };
        }
        private function closedInnerPane():void{
            this.m_isTopSlidePickOpened = false;
        }
        protected function onAccept(_arg1:Event):void{
            this.m_peddler.onAccept();
        }
        protected function onHavest(_arg1:Event):void{
            this.m_peddler.onHarvest();
        }
        protected function onExpel(_arg1:Event):void{
            this.m_peddler.onExpel();
        }
        private function removeButtonListeners():void{
            if (this.m_acceptBtn){
                this.m_acceptBtn.removeActionListener(this.onAccept);
            };
            if (this.m_havestBtn){
                this.m_havestBtn.removeActionListener(this.onHavest);
            };
        }
        protected function stopMousePropagation(_arg1:MouseEvent):void{
            _arg1.stopPropagation();
            _arg1.stopImmediatePropagation();
        }
        override public function cleanUp():void{
            this.stopUpdatePercentages();
            this.removeButtonListeners();
            super.cleanUp();
            removeEventListener(MouseEvent.MOUSE_MOVE, this.stopMousePropagation);
            removeEventListener(MouseEvent.MOUSE_OUT, this.stopMousePropagation);
            removeEventListener(MouseEvent.MOUSE_OVER, this.stopMousePropagation);
            removeEventListener(MouseEvent.CLICK, this.onMouseClick);
        }

    }
}//package Modules.peddler 
